package com.lianxi.Courses.controller;

import com.github.pagehelper.PageInfo;
import com.lianxi.Courses.constant.ResponseCodeConstant;
import com.lianxi.Courses.entity.Comment;
import com.lianxi.Courses.entity.Praise;
import com.lianxi.Courses.entity.User;
import com.lianxi.Courses.exception.RequestParameterErrorException;
import com.lianxi.Courses.service.CommentService;
import com.lianxi.Courses.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

/**
 * 翔哥
 * 2025年10月13日
 */
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @RequestMapping("/findAll")
    public ModelAndView findAll(Comment commentParam, String pageNo, String pageSize) {
        ModelAndView mav = new ModelAndView();
        PageInfo<Comment> commentPage = commentService.findPage(commentParam,pageNo, pageSize);
        mav.addObject("commentPage", commentPage);
        mav.setViewName("backend/back_commentManageSet");

        mav.addObject("commentParam",commentParam);
        return mav;
    }
    @RequestMapping("/enableById")
    public ResponseResult enableById(String id ) {
        ResponseResult result =new ResponseResult();
        try {
            commentService.enableById(id);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (Exception e) {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }

    @RequestMapping("/disableById")
    public ResponseResult disableById(String id ) {
        ResponseResult result =new ResponseResult();
        try {
            commentService.disableById(id);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (Exception e) {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }

    @RequestMapping("/findByResourceId")
    public ModelAndView findByResourceId(String resourceId, String pageNo, String pageSize) {
        ModelAndView mav = new ModelAndView();
        try {
            PageInfo<Comment> commentPage = commentService.findByResourceId(resourceId, pageNo, pageSize);
            mav.addObject("commentPage2", commentPage);
            mav.setViewName("backend/back_commentSet");
        } catch (RequestParameterErrorException e) {
            mav.addObject("message", e.getMessage());
            mav.setViewName("backend/back_commentSet");
        } catch (Exception e) {
            mav.addObject("message", "服务器内部异常");
            mav.setViewName("backend/back_commentSet");
        }
        return mav;
    }


    @RequestMapping("/modifyStatus")
    @ResponseBody
    public ResponseResult modifyStatus(String id, String status) {
        ResponseResult result = new ResponseResult();
        try {
            commentService.modifyStatus(id, status);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (Exception e) {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }

    //添加评论


    @RequestMapping("/addComment")
    public ResponseResult addComment(String context, String resourceId,HttpSession session) {
        ResponseResult result = new ResponseResult();
        try {
            User user = (User) session.getAttribute("user");
            String id = String.valueOf(user.getId());

            commentService.insertComment(id,context,resourceId);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (Exception e) {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }

    //点赞评论
    @RequestMapping("/addLike")
    public ResponseResult addLike(HttpSession session,String commentId) {
        ResponseResult result = new ResponseResult();
        try {
            User user = (User) session.getAttribute("user");
            String id = String.valueOf(user.getId());
            commentService.addLike(id,commentId);

            Praise praise = commentService.findPraise(id,commentId);
            if (praise==null){
                result.setMessage("点赞取消");
                result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
            }else {
                result.setMessage("点赞成功");
                result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
            }

        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (Exception e) {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }

}
